package com.etc.dao.impl;

import java.util.List;

import com.etc.dao.UserDao;
import com.etc.entity.User;
import com.etc.util.DBUtil;
import com.etc.util.PageData;

public class UserDaoImpl implements UserDao {

	@Override
	/**
	 * 通过账户密码查询用户的方法
	 */
	public User getUser(String userName, String userPwd) {
		String sql = "select * from user where userName=? and userPwd=?";
		List<User> list = (List<User>) DBUtil.select(sql, User.class, userName, userPwd);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	/**
	 * 分页加模糊查询用户列表的信息
	 */
	public PageData<User> getUserByPage(int page, int pageSize, String content) {
		// and userLevel>0
		String sql = "select * from user where userName like ? ";
		PageData<User> pageData = DBUtil.getPage(sql, page, pageSize, User.class, "%" + content + "%");
		return pageData;
	}

	/**
	 * 保存用户信息的方法
	 */
	@Override
	public int saveUser(User user) {
		String sql = "insert into user(userName,userPwd,userSex,userTel,userAddress,userLevel,userState) values(?,?,?,?,?,?,?)";
		int n = DBUtil.execute(sql, user.getUserName(), user.getUserPwd(), user.getUserSex(), user.getUserTel(),
				user.getUserAddress(), user.getUserLevel(), user.getUserState());
		return n;
	}

	/**
	 * 通过id获取用户信息的方法
	 */
	@Override
	public User getUserById(int userId) {
		String sql = "select * from user where userId = ?";
		List<User> list = (List<User>) DBUtil.select(sql, User.class, userId);
		if (list.size() > 0) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * 修改用户信息的方法
	 */
	@Override
	public int updateUser(User user) {
		String sql = "update user set userName=?,userSex=?,userTel=?,userAddress=?,userLevel=?,userState=?  where userId=?";
		int n = DBUtil.execute(sql, user.getUserName(), user.getUserSex(), user.getUserTel(), user.getUserAddress(),
				user.getUserLevel(), user.getUserState(), user.getUserId());
		return n;
	}

	/**
	 * 根据id删除用户的方法
	 */
	@Override
	public int delete(int userId) {
		String sql = "delete from  user where userId=?";
		int n = DBUtil.execute(sql, userId);
		return n;
	}
	
	/**
	 * 根据用户名查询用户的方法
	 */
	@Override
	public User finduser(String userName) {
		// TODO Auto-generated method stub
		String sql ="select * from user where userName=?";
		List<User> list= (List<User>) DBUtil.select(sql, User.class, userName);
		if(list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<User> getUserAll() {
		// TODO Auto-generated method stub
		String sql ="select * from user";
		List<User> list = (List<User>) DBUtil.select(sql, User.class);
		return list;
	}

}
